package com.example.administrator.daggerdemo.presenter;

import android.view.View;

import com.example.administrator.daggerdemo.base2.base.BaseObserver;
import com.example.administrator.daggerdemo.base2.base.BasePresenterImpl;
import com.example.administrator.daggerdemo.model.network.request.GetSMScodeRequestInfo;
import com.example.administrator.daggerdemo.model.network.request.RegisterRequestInfo;
import com.example.administrator.daggerdemo.model.network.response.GetSMSCodeResponseInfo;

import com.example.administrator.daggerdemo.model.network.response.RegisterResponseInfo;
import com.example.administrator.daggerdemo.view.IRegisterActivityView;

import org.json.JSONException;

import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * Created by zhengjian on 2016/12/13.
 */
public class RegisterActivityPresenter extends BasePresenterImpl<IRegisterActivityView> {
    /**
     * 构造方法
     *
     * @param view 具体业务的接口对象
     */
    public RegisterActivityPresenter(IRegisterActivityView view) {
        super(view);
    }


    /**
     * 获取验证码
     * @param telphone
     */
    public void getSMS(String telphone)
    {
        GetSMScodeRequestInfo info=new GetSMScodeRequestInfo();
        info.setUser_phone(telphone);
        info.setType("1");
        iView.showProgress();
        try {
            netWorkManger.doGetSMS(info).subscribeOn(Schedulers.io())
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe(new BaseObserver<GetSMSCodeResponseInfo>() {
                        @Override
                        public void onCompleted() {
                            iView.hideProgress();
                        }

                        @Override
                        public void onFail(String failMsg) {
                           iView.toast(failMsg);
                        }

                        @Override
                        public void onFail(int failId) {
                             switch (failId)
                             {
                                 case 0:
                                     iView.msgError("手机已经注册过了");
                                     break;
                             }
                        }

                        @Override
                        public void onSuccess(GetSMSCodeResponseInfo bean) {
                            iView.getSMSSuccess(bean);
                        }
                    });
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

    public void register(String telphone,String pwd,String code)
    {
        RegisterRequestInfo reginfo =new RegisterRequestInfo();
        reginfo.setUser_name(telphone);
        reginfo.setUser_password(pwd);
        reginfo.setVerify_code(code);
        iView.showProgress();
        try {
            netWorkManger.doRegister(reginfo).subscribeOn(Schedulers.io())
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe(new BaseObserver<RegisterResponseInfo>() {
                        @Override
                        public void onCompleted() {
                            iView.hideProgress();
                        }

                        @Override
                        public void onFail(String failMsg) {
                            iView.toast(failMsg);
                        }

                        @Override
                        public void onFail(int failId) {
                            switch (failId)
                            {
                                case 0:
                                    iView.msgError("手机已经注册过了");
                                    break;
                                case -2:
                                     iView.msgError("用户名错误不能为空");
                                    break;
                                case -3:
                                     iView.msgError("密码不能为空");
                                    break;
                                case -4:
                                     iView.msgError("短信验证码有误");
                                    break;
                            }
                        }

                        @Override
                        public void onSuccess(RegisterResponseInfo bean) {
                            iView.registerSuccess(bean);
                        }
                    });
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }
}
